/*
编写 : Hexh
时间 : 2012-9-18 16:20:45
*/
using System;
using System.ComponentModel;
using System.Web.Mvc;
using HMIS.FrameWork;
using HMIS.Infrastructure.Core.Entities;
using HMIS.Infrastructure.Facade;
using HMIS.Infrastructure.Facade.Authority;
using HMIS.MVC.CommonSupport;
using HMIS.MVC.ViewModel;

namespace HMIS.MVC.Controllers
{
	/// <summary>
	///	 the mvc class of CifCust
	/// </summary>
	public class CifCustController : BaseController
	{

		private readonly ICifCustFacade CifCustFacade;
		private readonly IHospUserFacade HospUserFacade;
		
		public CifCustController(ICifCustFacade CifCustFacade, IHospUserFacade HospUserFacade)
		{
			this.CifCustFacade = CifCustFacade;
			this.HospUserFacade = HospUserFacade;
		}
		
		[ExtResult]
		[Description("添加修改CifCust信息")]
		public ActionResult SaveOrUpdate()
		{
			CifCust entity;
			if (String.IsNullOrEmpty(Request["Id"]))
			{
				entity = new CifCust
				{
					//CustNo = Request["CustNo"]==null ? 0 : int.Parse(Request["CustNo"]), 
					CustNa = Request["CustNa"], 
					CustPy = Request["CustPy"], 
					CustTp = Request["CustTp"], 
					CertTp = Request["CertTp"], 
					CertNo = Request["CertNo"], 
					ContUs = Request["ContUs"], 
					OffiTl = Request["OffiTl"], 
					HomeTl = Request["HomeTl"], 
					MobiTl = Request["MobiTl"], 
					MailCd = Request["MailCd"], 
					MailAd = Request["MailAd"], 
					EMail = Request["EMail"], 
				};
			}
			else
			{
				// get the entity.
				entity = CifCustFacade.Get(Request["Id"]);
				// set new value..
				//entity.CustNo = Request["CustNo"]==null ? 0 : int.Parse(Request["CustNo"]); 
				entity.CustNa = Request["CustNa"]; 
				entity.CustPy = Request["CustPy"]; 
				entity.CustTp = Request["CustTp"]; 
				entity.CertTp = Request["CertTp"]; 
				entity.CertNo = Request["CertNo"]; 
				entity.ContUs = Request["ContUs"]; 
				entity.OffiTl = Request["OffiTl"]; 
				entity.HomeTl = Request["HomeTl"]; 
				entity.MobiTl = Request["MobiTl"]; 
				entity.MailCd = Request["MailCd"]; 
				entity.MailAd = Request["MailAd"]; 
				entity.EMail = Request["EMail"]; 
			}			
			// TODO ..设置外键相关信息..
			/*
			string[] authusers = Request["AuthorizeUsers"].Split(',');
			for (int i = 0; i < authusers.Length; i++)
			{
				entity.AuthorizeUsers.Add(HospUserFacade.Load(authusers[i]));
			}
			*/
			// 保存信息
			CifCustFacade.SaveOrUpdate(entity);

			return this.JsonFormat(new ExtResult { success = true, id = entity.Id });
		}
		
		[ExtResult]
		[Description("删除CifCust基本资料")]
		public ActionResult Delete()
		{
			CifCustFacade.Delete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}
		
		[ExtResult]
		[Description("物理删除CifCust基本资料")]
		public ActionResult PhysicsDelete()
		{
			CifCustFacade.PhysicsDelete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}

		[Description("获取CifCust清单")]
		public ActionResult GetPlist()
		{
			long total;
			var data = CifCustFacade.GetPlist(
				Request["q_query"],
				int.Parse(Request["start"] == null ? "0" : Request["start"]),
				int.Parse(Request["limit"] == null ? "0" : Request["limit"]),
				Request["sort"],
				Request["dir"],
				this.DesDataFilter(Request["filter"]),
				out total);
			//TODO : 修改关联查询返回的类型
			return this.JsonFormat(
				new ExtGirdData<CifCust> { total = total, data = data },
				new string[] { "CustomType" });
		}
		
		[Description("获取CifCust信息")]
		public ActionResult GetById()
		{
			var entity = CifCustFacade.Get(Request["Id"]);
			return this.JsonFormat(
			   new ExtFormLoadData<CifCust> { data = new CifCust[] { entity } }, new string[] { "AuthorizeUsers" }
			   );
		}

		[ExtResult]
		[Description("判断CifCust字段是否唯一")]
		public ActionResult IsFieldExist()
		{
			var result = CifCustFacade.IsFieldExist(Request["FieldName"], Request[Request["FieldName"]], Request["Id"]);
			return this.JsonFormat(new ExtResult { success = !result, msg = "已存在相同的项!" });
		}
	}
}
